const path = require('path');
const uglifyJs = require('uglifyjs-webpack-plugin');//用来压缩js文件
const CleanWebpackPlugin = require('clean-webpack-plugin').CleanWebpackPlugin;

module.exports = {
    mode: 'development',
    entry: './index.js',
    output: {
        filename: 'index.js',//打包出来的入口js文件,只显示8位
        path: path.resolve(__dirname, 'dist') //打包出来的文件的路径
    },
    resolve: {
        extensions: ['.js'],
        alias: {
            src: path.resolve(__dirname, 'src'),
        }
    },
    devServer: {//开发服务器配置
        port: 3000,//设置服务的端口号
        progress: true,
        contentBase: './dist', //指定服务的默认目录
        compress: false,//是否压缩文件
        open: true,//是否自动打开浏览器
    },
    plugins: [
        new CleanWebpackPlugin()
    ],
    optimization: {//优化项
        minimizer: [
            new uglifyJs(),
        ]
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                use: [
                    {
                        //使用babel-loader将es6转化为es5
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                '@babel/preset-env',
                            ],
                            // plugins: [
                            //     "syntax-dynamic-import",
                            // ]
                        }
                    },
                ]
            },
            {
                test: /\.scss$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'sass-loader'
                ]
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader',
                ]
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [
                  {
                    loader: 'url-loader',
                    options: {
                      limit: 20000
                    }
                  }
                ]
              }
        ]
    }
}